package com.clqteam.daoimpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.clqteam.dao.showShDao;
import com.clqteam.model.Product;
import com.clqteam.util.JDBCutil;

public class showShDaoImpl implements showShDao {
	@Override
	public int getProductCount() {
		int count = 0;
		StringBuffer sql = new StringBuffer();
		sql.append(" SELECT COUNT(s_product.pId) AS pcount");
		sql.append(" FROM s_product");
		Connection con = JDBCutil.getConnection();
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			ResultSet rst = pst.executeQuery();
			if (rst.next()) {
				count = rst.getInt("pcount");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return count;
	}

	@Override
	public List<Product> getAllProduct(int page,int rows) {
		List<Product> pros = new ArrayList<>();
		StringBuffer sql = new StringBuffer();
		sql.append(" SELECT * FROM s_product");
		sql.append(" ORDER BY s_product.pName");
		sql.append(" LIMIT ?,?");
		Connection con = JDBCutil.getConnection();
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			pst.setInt(1,(page-1)*rows);
			pst.setInt(2, rows);
			ResultSet rst = pst.executeQuery();
			while (rst.next()) {
				Product p = new Product();
				p.setpId(rst.getInt("pId"));
				p.setpName(rst.getString("pName"));
				p.setpType(rst.getString("pType"));
				p.setpAmount(rst.getInt("pAmount"));
				p.setpClass(rst.getString("pClass"));
				p.setpCost(rst.getDouble("pCost"));
				p.setpPrice(rst.getDouble("pPrice"));
				p.setpTime(rst.getString("pTime"));
				p.setSupId(rst.getInt("supId"));
				pros.add(p);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return pros;
	}
}
